﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Scheduling.Interfaces.ViewModels;
using Presentation.Module.Scheduling.Interfaces.Services;
using Presentation.Module.StudyDefinition.Interfaces.Services;
using Presentation.Module.Scheduling.Interfaces.Views;
using Microsoft.Practices.Prism.Regions;
using Presentation.Module.Scheduling.ViewDto;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using System.ComponentModel;

namespace Presentation.Module.Scheduling.ViewModels
{

    public class AddScheduleStudyViewModel : IAddScheduleStudyViewModel, INotifyPropertyChanged 
    {
        private ScheduleStudy _CurrentScheduleStudy;
        private List<Presentation.Module.StudyDefinition.Models.Study> _Studys;
        
        
        public AddScheduleStudyViewModel(IScheduleStudyService scheduleStudyService, IRegionManager regionManager, IStudiesUIProxyService studiesService)
        {
            this.ScheduleStudyService = scheduleStudyService;
            this.StudiesService = studiesService;
            this.RegionManager = regionManager;
            this.Studys = new List<Presentation.Module.StudyDefinition.Models.Study>();
            this.Commit = new DelegateCommand(OnCommit);
            this.Cancel = new DelegateCommand(OnCancel);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand Commit { get; set; }
        public ICommand Cancel { get; set; }

        // List of Studys - View binds to these
        public List<Presentation.Module.StudyDefinition.Models.Study> Studys
        {
            get
            {
                this._Studys = this.StudiesService.GetAllStudies().ToList();
                return _Studys;
            }
            set
            {
                if (value != null)
                {
                    _Studys = value;
                    //RaiseOnPropertyChanged("Studys");
                }
            }
        }

        public ScheduleStudy CurrentScheduleStudy
        {
            get
            {
                return _CurrentScheduleStudy;
            }
            set
            {
                if (value != null)
                {
                    _CurrentScheduleStudy = value;
                    RaiseOnPropertyChanged("CurrentScheduleStudy");
                }
            }
        }

        private IScheduleStudyService ScheduleStudyService { get; set; }
        private IStudiesUIProxyService StudiesService { get; set; }
        private IRegionManager RegionManager { get; set; }

        private void OnCommit()
        {
            this.ScheduleStudyService.AddScheduleStudy(this.CurrentScheduleStudy);
            //this.ScheduleStudyService.AddScheduleStudy(this.CurrentScheduleStudy);
            this.CurrentScheduleStudy = new ScheduleStudy();
            this.RaiseOnPropertyChanged("CurrentScheduleStudy");
        }

        private void OnCancel()
        {
            this.RegionManager.Regions["MainRegion"].Deactivate(this.View);
        }

        private void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public Presentation.Module.Scheduling.Interfaces.Views.IAddScheduleStudyView View { get; set; }
    }
}
